enum CacheTypeEnum {
    LOCAL,
    SESSION
}

class Cache {

    storage: Storage

    constructor(type: CacheTypeEnum) {
        this.storage = type === CacheTypeEnum.LOCAL ? localStorage : sessionStorage
    }

    set(key: string, value: any) {
        if (value) {
            this.storage.setItem(key, JSON.stringify(value))
        }
    }

    get(key: string) {
        const value = this.storage.getItem(key)
        if (value) {
            return JSON.parse(value)
        }

    }

    remove(key: string) {
        this.storage.removeItem(key)
    }

    clear() {
        this.storage.clear()
    }

}

const localCache = new Cache(CacheTypeEnum.LOCAL)
const sessionCache = new Cache(CacheTypeEnum.SESSION)

export {
    localCache, sessionCache
}
